package com.soboot.collect.provider.processor;

import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.Spider;
import us.codecraft.webmagic.pipeline.ConsolePipeline;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Html;

import java.util.List;

/**
 * @Author:
 * @createTime: 2022年12月09日 15:19:10
 * @version:
 * @Description:
 * @Copyright:
 */
public class PrivatePageProcessor implements PageProcessor {

    private Site site = Site.me().setDomain("my.oschina.net");
    //private Site site = Site.me().setDomain("https://weibo.com/newlogin?tabtype=weibo&gid=1028034288&openLoginLayer=0&url=");

    @Override
    public void process(Page page) {
        Html html = page.getHtml();
        List<String> links = page.getHtml().links().regex("http://my\\.oschina\\.net/jiagoushi/blog/\\d+").all();
        page.addTargetRequests(links);
        page.putField("title", page.getHtml().xpath("//div[@class='BlogEntity']/div[@class='BlogTitle']/h1").toString());
        //page.putField("content", page.getHtml().$("div.content").toString());
        page.putField("content", page.getHtml().toString());
        page.putField("tags",page.getHtml().xpath("//div[@class='BlogTags']/a/text()").all());
    }

    @Override
    public Site getSite() {
        return site;

    }

    public static void main(String[] args) {
        String html = "http://my.oschina.net/flashsword/blog";
        Spider.create(new PrivatePageProcessor()).addUrl("http://tieba.baidu.com/f?ie=utf-8&kw=%E9%83%91%E7%A7%80%E5%A6%8D")
                .addPipeline(new ConsolePipeline()).run();
    }
}
